/*
** Copyright (C) 2009 Joshua Brent Marsh <joshua@icub3d.com>
**  
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 3 of the License, or
** (at your option) any later version.
**  
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**  
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**  
*/

/* $Id: map.c 9 2009-10-08 15:50:15Z joshua@icub3d.com $ */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <assert.h>

#include "map.h"

/* Helper function that checks the insertion of a node in the set. If it failed
   0 is returned. If it succeeded, 1 is returned. It's possible to succeed if
   a pair was already found in the set. In this case, the value in the set
   is updated and memory is cleaned up. 

   This is a helper for insert and union. */
int
check_insert (SSPair *found, SSPair *nnode)
{
  if (found == NULL) /* Failed to insert! */
    {
      dest_sspair (nnode);
      return 0;
    }
  else if (found != nnode) /* We already have that name in the map, replace. */
    {
      /* Replace the values and clean up memory. */
      free (found->value);
      found->value = nnode->value;
      free (nnode->name);
      free (nnode);

      return 1;
    }
  else /* Insertion succeeded normally. */
    return 1;
}

int
map_insert (Map *map, char *name, char *value)
{
  SSPair *nnode, *found;

  assert (map != NULL);

  nnode = create_sspair (name, value);
  if (nnode == NULL)
    return 0;

  found = set_add (map, nnode);
  return check_insert (found, nnode);
}

int
map_remove (Map *map, char *name)
{
  SSPair search, *found;

  assert (map != NULL);

  search.name = name;

  found = set_remove (map, &search);

  if (found == NULL)
    return 0;
  else
    {
      dest_sspair (found);
      return 1;
    }
}

char *
map_get (Map *map, char *name)
{
  SSPair search, *found;

  assert (map != NULL);

  search.name = name;

  found = set_search (map, &search);

  if (found == NULL)
    return NULL;
  else
    return found->value;
}

int
map_exists (Map *map, char *name)
{
  SSPair search, *found;

  assert (map != NULL);

  search.name = name;

  found = set_search (map, &search);

  if (found == NULL)
    return 0;
  else
    return 1;
}

int
map_union (Map *dest, Map *src)
{
  SetTraverser trav;
  SSPair *cur, *found, *nnode;
  
  assert (dest != NULL);
  assert (src != NULL);

  set_traverser_init (&trav, src);
  while ((cur  = set_traverser_next (&trav)) != NULL)
    {
      nnode = create_sspair (cur->name, cur->value);
      if (nnode == NULL)
	return 0;

      found = set_add (dest, nnode);

      if (!check_insert (found, nnode))
	return 0;
    }

  return 1;
}
